package com.yonja.zbox.common.utils;

import cn.hutool.core.util.StrUtil;
import com.yonja.zbox.common.constant.CommonConstant;
import com.yonja.zbox.common.constant.SecurityConstant;
import com.yonja.zbox.modules.system.dto.UserInfo;
import com.yonja.zbox.modules.system.entity.SysRole;
import com.yonja.zbox.modules.system.service.SysRoleService;
import com.yonja.zbox.modules.system.service.SysUserService;
import com.yonja.zbox.modules.system.vo.MenuVO;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author yonja
 */
@Component
@Slf4j
public class SecurityUtil {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleService roleService;

    /**
     * 获取当前登录用户
     * @return
     */
    public UserInfo getCurrUser(){

        UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return userService.getByUsername(user.getUsername());
    }

    /**
     * 获取当前用户数据权限 null代表具有所有权限
     */
//    public List<MenuVO> getDeparmentIds(){
//
//        List<MenuVO> deparmentIds = new ArrayList<>();
//        UserInfo u = getCurrUser();
//        // 用户角色
//        List<SysRole> userRoleList = roleService.listRolesByUserId(u.getUserId());
//        // 判断有无全部数据的角色
//        Boolean flagAll = false;
//        for(SysRole r : userRoleList){
//            if(r.getDataType()==null||r.getDataType().equals(CommonConstant.DATA_TYPE_ALL)){
//                flagAll = true;
//                break;
//            }
//        }
//        if(flagAll){
//            return null;
//        }
//        // 查找自定义
//        return iUserRoleService.findDepIdsByUserId(u.getId());
//    }

    /**
     * 通过用户名获取用户拥有权限
     * @param username
     */
    public List<GrantedAuthority> getCurrUserPerms(String username){

        List<GrantedAuthority> authorities = new ArrayList<>();
        for(MenuVO menu : userService.getByUsername(username).getPermissions()){
            authorities.add(new SimpleGrantedAuthority(menu.getName()));
        }
        return authorities;
    }

    /*********************************************************/

    /**
     * 获取Authentication
     */
    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取用户
     */
//    public ZboxUser getUser(Authentication authentication) {
//        Object principal = authentication.getPrincipal();
//        if (principal instanceof ZboxUser) {
//            return (ZboxUser) principal;
//        }
//        return null;
//    }

    /**
     * 获取用户
     */
//    public ZboxUser getUser() {
//        Authentication authentication = getAuthentication();
//        if (authentication == null) {
//            return null;
//        }
//        return getUser(authentication);
//    }

    /**
     * 获取用户角色信息
     *
     * @return 角色集合
     */
    public List<SysRole> getRoles() {
        UserInfo u = getCurrUser();
        // 用户角色
        List<SysRole> userRoleList = roleService.listRolesByUserId(u.getUserId());
        return userRoleList;
    }
}
